package com.al.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class SignInServlet implements Servlet {
	private ServletConfig config;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		System.out.println("===初始化===");
		//Servlet 容器（也就是 Tomcat）通过初始化方法向 Servlet 实例传递数据
		//请注意，Servlet 容器通过 ServletConfig 对象 向 Servlet 实例传递数据
		System.out.println(this);
		this.config = config;
	}
	
	@Override
	public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
		System.out.println("===提供服务===");
		System.out.println(this);
		System.out.println(config.getServletName());
		
		//获取一个字符打印流（来自 io 包）
		PrintWriter out = resp.getWriter();
		out.print("<div style='test-align:center; border: 1px solid blue; padding: 10px'>");
		out.print(this);
		out.print("</div>");
	}


	@Override
	public void destroy() {
		System.out.println("===销毁（释放资源）===");
		System.out.println(this);
	}

	@Override
	public ServletConfig getServletConfig() {
		return this.config;
	}

	@Override
	public String getServletInfo() {
		//直接返回 config 中 当前 Servlet 实例的名称
		return config.getServletName();
	}

}
